* Results and output for tables
clear all
set mem 3000m
set matsize 5500

* globals
*global base "C:\Users\chari\Documents\rainfall project\"
global base "C:\research\factories\"
global do "C:\research\factories\do"
global data "C:\research\factories\data"
global output "C:\research\factories\output\restat"
global distcontrols c.riceareaper80#c.shockpctile c.cultarea#c.shockpctile c.wheatareaper80#c.shockpctile c.delicense88#c.shockpctile c.wage88#c.shockpctile c.kbyl88#c.shockpctile c.food88#c.shockpctile c.agrarian88#c.shockpctile c.landless88#c.shockpctile c.kqratio88#c.shockpctile c.cmhdlft88#c.shockpctile i.year
global aghioncontrols c.riceareaper80#c.shockpctile c.cultarea#c.shockpctile c.wheatareaper80#c.shockpctile c.delicense80#c.shockpctile c.wage80#c.shockpctile c.cmhdlft80#c.shockpctile i.year

set more off

* merge, prep vars etc.
do "$base\do\restat\prep.do"

* Table 1: Data sources.

/* table 2: labor regulations by state */
* bys strictness: tab agrstate

/* table 3a and 3b: descriptive stats by strict/lax states */
bys strictness: sum yield workers mandays materials fuel valueadded profits
bys strictness: tab shockpctile

/* table 4: agr prod and hh expenditure using only years included in the aghion panel */
areg agrproduction agrproduction, absorb(asicode88) cluster(asicode88) r
outreg2 using "$output\qt4.xls", replace
foreach x in agrproduction wage yield pcy {
	areg `x' shockpct i.year if year>1979, absorb(asicode88) cluster(asicode88) r
	outreg2 using "$output\qt4.xls", append
	}
foreach x in agrproduction wage yield pcy {
	areg `x' shockpct prow proe prow_shockpctile proe_shockpctile c.riceareaper80#c.shockpctile c.cultarea#c.shockpctile c.wheatareaper80#c.shockpctile i.year if year>1979, absorb(asicode88) robust cluster(asicode88)
	lincom proe_shockpctile - prow_shockpctile
	local est1 = r(estimate)
	local se1 = r(se)
	local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
	outreg2 using "$output\qt4.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(2) tdec(2) append
	}

/* table 5: main labor results (using district panel...state-ind results appended later) */
areg workers workers, absorb(asicode88) cluster(asicode88) r
outreg2 using "$output\qt5.xls", replace
foreach x in workers mandays {
	areg `x' shockpct i.year if year>1979, absorb(asicode88) cluster(asicode88) r
	outreg2 using "$output\qt5.xls", append
	}
foreach x in workers mandays {
	areg  `x' shockpct prow proe prow_shockpctile proe_shockpctile $distcontrols, absorb(asicode88) robust cluster(asicode88)
	lincom proe_shockpctile-prow_shockpctile
	local est1 = r(estimate)
	local se1 = r(se)
	local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
	outreg2 using "$output\qt5.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}


* state-industry panel results
do "$base\do\restat\prep_aghion.do"

* (appends to Table 5) main results with workers and employees in state-industry panel
foreach x in workers employees {
	xi: areg `x' shockpctile i.year, abs(statenic3) cluster(statenic3) r
	outreg2 using "$output\qt5.xls", append
	}

foreach x in workers employees {
	xi: areg `x' shockpctile prow_shockpctile proe_shockpctile proe prow $aghioncontrols, abs(statenic3) cluster(statenic3) r
		lincom proe_shockpctile - prow_shockpctile
		local est1 = r(estimate)
		local se1 = r(se)
		local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
		outreg2 using "$output\qt5.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}

* Table 6: main results with industry-rainshock FE
areg workers workers, absorb(statenic3) cluster(statenic3) r
outreg2 using "$output\qt6.xls", replace

foreach x in workers employees {
	xi: areg `x' shockpctile prow_shockpctile proe_shockpctile proe prow $aghioncontrols i.shockind, abs(statenic3) cluster(statenic3) r
		lincom proe_shockpctile - prow_shockpctile
		local est1 = r(estimate)
		local se1 = r(se)
		local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
		outreg2 using "$output\qt6.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}
	
* Table 7: results for output, value added and profits
do "$base\do\restat\prep.do"

xi: areg workers workers i.year, abs(asicode88) cluster(asicode88) r
outreg2 using "$output\qt7.xls", replace

foreach x of varlist totaloutput valueadded profits {
	areg `x' shockpctile i.year, absorb(asicode88) cluster(asicode88) r
	outreg2 using "$output\qt7.xls", append
	}
foreach x of varlist totaloutput valueadded profits {
	areg  `x' shockpct prow proe prow_shockpctile proe_shockpctile $distcontrols, absorb(asicode88) robust cluster(asicode88)
		lincom proe_shockpctile - prow_shockpctile
		local est1 = r(estimate)
		local se1 = r(se)
		local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
		outreg2 using "$output\qt7.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}


* Table 8: some tests of explanations for the 0 effect on profits
xi: areg workers workers i.year, abs(asicode88) cluster(asicode88) r
outreg2 using "$output\qt8.xls", replace

foreach x in fixedcapitalcl materials fuel electricity factories factwage manwork outwork {
	areg  `x' shockpct prow proe prow_shockpctile proe_shockpctile $distcontrols, absorb(asicode88) robust cluster(asicode88)
	lincom proe_shockpctile - prow_shockpctile
	local est1 = r(estimate)
	local se1 = r(se)
	local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
	outreg2 using "$output\qt8.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}

	

/* app table 1: separate regs by small/med/large factories */
do "$base\do\restat\prep_small_medium_large.do"

areg workers workers, absorb(asicode88) cluster(asicode88) r
outreg2 using "$output\appqt1.xls", replace
foreach y in 1 2 3 {
	foreach x in workers mandays {
		xi: areg `x' shockpctile prow proe prow_shockpctile proe_shockpctile $distcontrols if size==`y', absorb(asicode) robust cluster(asicode88)
		lincom proe_shockpctile - prow_shockpctile
			local est1 = r(estimate)
			local se1 = r(se)
			local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
			outreg2 using "$output\appqt1.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
		}
	}

/* app table 2 - small/med/large triple interactions */
areg workers workers, absorb(asicode88) cluster(asicode88) r
outreg2 using "$output\appqt2.xls", replace
foreach x in workers mandays {
	xi: areg `x' shockpct_prow_medium shockpct_proe_medium shockpct_prow_large shockpct_proe_large prow_shockpctile proe_shockpctile shockpct_medium shockpct_large prow_medium prow_large proe_medium proe_large prow proe medium large shockpctile i.shocksize $distcontrols, absorb(ds) robust cluster(asicode88)
		lincom shockpct_proe_large - shockpct_prow_large
		local est1 = r(estimate)
		local se1 = r(se)
		local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
		lincom shockpct_proe_medium - shockpct_prow_medium
		local est2 = r(estimate)
		local se2 = r(se)
		local pval2 = tprob(r(df), abs(r(estimate)/r(se)))
		outreg2 using "$output\appqt2.xls", excel addstat(large_diff, `est1', SE1, `se1', pval1, `pval1', medium_diff, `est2', SE2, `se2', pval2, `pval2') bdec(3) tdec(3) append
	}

/* App Table 3: regs stratified by "twonic": shows that differential results are bigger in 200 category */
do "$base\do\restat\prep_aghion.do"

xi: areg workers workers i.year, abs(statenic3) cluster(statenic3) r
outreg2 using "$output\appqt3.xls", replace

foreach y in 1 0 {
	foreach x in workers employees {
		xi: areg `x' shockpctile i.year if twonic==`y', abs(statenic3) cluster(statenic3) r
		outreg2 using "$output\appqt3.xls", append
		}
		
	foreach x in workers employees {
		xi: areg `x' shockpctile proe_shockpctile prow_shockpctile proe prow $aghioncontrols if twonic==`y', abs(statenic3) cluster(statenic3) r
			lincom proe_shockpctile - prow_shockpctile
			local est1 = r(estimate)
			local se1 = r(se)
			local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
			outreg2 using "$output\appqt3.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
		}
	}

* App Table 4: triple interaction with "twonic"
xi: areg workers workers i.year, abs(statenic3) cluster(statenic3) r
outreg2 using "$output\appqt4.xls", replace

foreach x in workers employees {
	xi: areg `x' twonic_shockpctile shockpctile proe prow $aghioncontrols i.year, abs(statenic3) cluster(statenic3) r
	outreg2 using "$output\appqt4.xls", append
	}

foreach x in workers employees {
	xi: areg `x' twonic* shockpctile proe_shockpctile prow_shockpctile proe prow $aghioncontrols, abs(statenic3) cluster(statenic3) r
		lincom twonic_proe_shockpctile - twonic_prow_shockpctile
		local est1 = r(estimate)
		local se1 = r(se)
		local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
		outreg2 using "$output\appqt4.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}

* App Table 5: replicating main results using alternative labor regulation measures
do "$base\do\restat\prep.do"

* district panel:
xi: areg workers workers i.year, abs(asicode88) cluster(asicode88) r
outreg2 using "$output\appqt5.xls", replace

areg workers shockpct EPLprow EPLprow_shockpctile $distcontrols, absorb(asicode88) robust cluster(asicode88)
outreg2 using "$output\appqt5.xls", append

foreach x in Bhatt DS {
	areg workers shockpct `x'prow `x'proe `x'prow_shockpctile `x'proe_shockpctile $distcontrols, absorb(asicode88) robust cluster(asicode88)
		lincom `x'proe_shockpctile - `x'prow_shockpctile
		local est1 = r(estimate)
		local se1 = r(se)
		local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
		outreg2 using "$output\appqt5.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}

* state-industry panel:
do "$base\do\restat\prep_aghion.do"

xi: areg workers shockpctile EPLprow EPLprow_shockpctile $aghioncontrols, abs(statenic3) cluster(statenic3) r
	outreg2 using "$output\appqt5.xls", append

foreach x in B DS {
	xi: areg workers shockpctile `x'prow_shockpctile `x'proe_shockpctile `x'proe `x'prow $aghioncontrols, abs(statenic3) cluster(statenic3) r
		lincom `x'proe_shockpctile - `x'prow_shockpctile
		local est1 = r(estimate)
		local se1 = r(se)
		local pval1 = tprob(r(df), abs(r(estimate)/r(se)))
		outreg2 using "$output\appqt5.xls", excel addstat(difference, `est1', SE, `se1', pval, `pval1') bdec(3) tdec(3) append
	}



	
	

/* end do file */
